package com.zhou;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * TestController
 *
 * @author zhouxianfeng
 * @date 2021-5-26
 */
@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    private StreamClient streamClient;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping
    @Transactional(rollbackFor = Exception.class)
    public void test() {
        streamClient.output().send(MessageBuilder.withPayload("Hello World...").build());
        if (1 == 1) {
            throw new RuntimeException("");
        }
    }

    @GetMapping("/1")
    public void test1() {
        streamClient.output().send(MessageBuilder.withPayload("Hello World...").build());
        if (1 == 1) {
            throw new RuntimeException("");
        }
    }

    @GetMapping("/transaction")
    @Transactional(rollbackFor = Exception.class)
    public void transaction() {
        rabbitTemplate.convertAndSend("Hello World...");
        if (1 == 1) {
            throw new RuntimeException("");
        }
    }
}
